package io.litchi.auth.Infrastructure.handler;

import io.litchi.auth.Infrastructure.constant.AuthConstant;
import io.litchi.auth.Infrastructure.util.ResponseUtil;
import io.litchi.common.result.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import java.io.IOException;

/**
 * description: 自定义处理403 无权限
 *
 * @author helang
 * @since 2025-09-11
 */
public class LitchiAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {

        Result<Void> result = Result.fail(AuthConstant.AUTHORIZATION_MESSAGE);
        ResponseUtil.writeJsonResponse(response, HttpStatus.FORBIDDEN.value(), result);
    }
}